﻿-- Author      : Kurapica

--------------------------
-- Form		        --
--------------------------
do
	-- Check Version
	local version = 1

	if not IGAS:RegisterFile("IGAS.GUI.Widget.Form", version) then
		return
	end

	local getmetatable = getmetatable
	local pairs = pairs
	local ipairs = ipairs
	local type = type
	local error = error
    local CreateFrame = IGAS.GUI.CreateFrame

	local _WidgetName = "Form"
	local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
	local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType

	-- Script
	local _FrameBackdrop = {
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}

	local function frameOnMouseDown(self)
		if not self.Parent.InDesignMode then
			self:GetParent():StartMoving()
		end
	end

	local function frameOnMouseUp(self)
		self:GetParent():StopMovingOrSizing()
	end

	local function sizerseOnMouseDown(self)
		if not self.Parent.InDesignMode and self:GetParent().Resizable then
			self:GetParent():StartSizing("BOTTOMRIGHT")
		end
	end

	local function sizersOnMouseDown(self)
		if not self.Parent.InDesignMode and self:GetParent().Resizable then
			self:GetParent():StartSizing("BOTTOM")
		end
	end

	local function sizereOnMouseDown(self)
		if not self.Parent.InDesignMode and self:GetParent().Resizable then
			self:GetParent():StartSizing("RIGHT")
		end
	end

	local function sizerOnMouseUp(self)
		self:GetParent():StopMovingOrSizing()
	end

	-- ScriptType
	local _ScriptType = {
	}

	--	FuncProxy
	local _FuncProxy = {
		-- GetCaption
		["GetCaption"] = function(self)
			return self:GetChild("Title"):GetChild("Text"):GetText()	
		end,

		-- SetCaption		
		["SetCaption"] = function(self, title)
			self:GetChild("Title"):GetChild("Text"):SetText(title)
		end,
        
        -- GetMessage
        ["GetMessage"] = function(self)
            return self:GetChild("StatusBar"):GetChild("Text"):GetText() or ""
        end,
        
		-- SetMessage
		["SetMessage"] = function(self, mes)
			self:GetChild("StatusBar"):GetChild("Text"):SetText(mes)
		end,
	}

	--	Property
	local _Property = {
		-- Caption
		["Caption"] = {
			["Set"] = function(self, title)
				self:SetCaption(title)
			end,

			["Get"] = function(self)
				return self:GetCaption()
			end,

			["Type"] = _PropertyType.String,
		},
		-- Message
		["Message"] = {
			["Set"] = function(self, mes)
				self:SetMessage(mes)
			end,

			["Get"] = function(self)
				return self:GetMessage()
			end,

			["Type"] = _PropertyType.String,
		},
	}

	local function _New(parent)		
		-- New Frame
		local frame = CreateFrame("Frame",nil,parent)

		frame.Width = 700
		frame.Height = 500
		frame.Movable = true
		frame.Resizable = true
		frame.FrameStrata = "DIALOG"
		frame.Toplevel = true

		frame:SetPoint("CENTER",parent,"CENTER",0,0)
		frame:SetBackdrop(_FrameBackdrop)
		frame:SetBackdropColor(0,0,0,1)
		frame:SetMinResize(400,200)

		local CloseButton = CreateFrame("Button","CloseBtn",frame)
		CloseButton:SetPoint("TOPRIGHT",frame,"TOPRIGHT")
        CloseButton.Style = "CLOSE"
		
		local title = CreateFrame("Frame","Title",frame)
		title.MouseEnabled = true
		title.Height = 24
		title:SetPoint("TOPLEFT",frame,"TOPLEFT")
		title:SetPoint("TOPRIGHT",CloseButton,"TOPRIGHT")
		title.OnMouseDown = frameOnMouseDown
		title.OnMouseUp = frameOnMouseUp

		local titletext = CreateFrame("FontString","Text",title,"OVERLAY","GameFontNormal")
		titletext:SetPoint("CENTER",title,"CENTER")
		titletext:SetText("Form")

        local status = CreateFrame("Frame","StatusBar",frame)
        status.Height = 24
        status:SetPoint("BOTTOMLEFT",frame,"BOTTOMLEFT", 6, 0)
        status:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT")
        
        local statusText = CreateFrame("FontString","Text",status,"OVERLAY","GameFontNormal")
        statusText.JustifyH = "LEFT"
        statusText:SetAllPoints(status)
        statusText:SetText("")

		local sizer_se = CreateFrame("Frame","Sizer_se",frame)
		sizer_se.Width = 25
		sizer_se.Height = 25
		sizer_se.MouseEnabled = true
		sizer_se:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",0,0)
		sizer_se.OnMouseDown = sizerseOnMouseDown
		sizer_se.OnMouseUp = sizerOnMouseUp

		local sizer_s = CreateFrame("Frame","Sizer_s",frame)
		sizer_s:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",-25,0)
		sizer_s:SetPoint("BOTTOMLEFT",frame,"BOTTOMLEFT",0,0)
		sizer_s.Height = 25
		sizer_s.MouseEnabled = true
		sizer_s.OnMouseDown = sizersOnMouseDown
		sizer_s.OnMouseUp = sizerOnMouseUp

		local sizer_e = CreateFrame("Frame","Sizer_e",frame)
		sizer_e:SetPoint("BOTTOMRIGHT",frame,"BOTTOMRIGHT",0,25)
		sizer_e:SetPoint("TOPRIGHT",frame,"TOPRIGHT",0,0)
		sizer_e.Width = 25
		sizer_e.MouseEnabled = true
		sizer_e.OnMouseDown = sizereOnMouseDown
		sizer_e.OnMouseUp = sizerOnMouseUp

		frame.MouseWheelEnabled = true
		frame.MouseEnabled = true

		return frame
	end	

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["FuncProxy"] = _FuncProxy,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
	}

	_Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
end
